﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace ScarEngine
{
    public abstract class GameObject
    {

        //public List<Component> Components;
        public Dictionary<string, Component> Components;
        private bool m_DeleteFlag;
        //public List<> Assets;

        public GameObject()
        {
            Components = new Dictionary<string, Component>();
            MasterManager.GameObjects.Add(this);
        }



        public virtual void Update(GameTime gameTime)
        {
            foreach (KeyValuePair<string, Component> component in Components)
            {
                if (component.Value.init == true)
                {
                    component.Value.Init();
                }
                else
                {
                    component.Value.Update(gameTime);
                }
            }
        }







        public ComponentType Get<ComponentType>() where ComponentType : Component
        {
            foreach (KeyValuePair<string, Component> pair in Components)
            {
                if (pair.Value is ComponentType)
                    return pair.Value as ComponentType;
            }

            return null;
        }



        public ComponentType Add<ComponentType>(string name) where ComponentType : Component, new()
        {

            Component c = ComponentCreator.CreateComponent<ComponentType>();
            c.Name = name;
            c.Object = this;
            c.Prerequisites();
            c.ExposeMethods();
            Components.Add(name, c);
            return c as ComponentType;
        }



        public bool HasComponent<ComponentType>()
        {
            foreach (KeyValuePair<string, Component> pair in Components)
            {
                if (pair.Value is ComponentType)
                    return true;
            }
            return false;
        }



        public void Remove(string name)
        {
            if (Components.ContainsKey(name))
            {
                Component c = Components[name];
                Components.Remove(name);
                c.UnExposeMethods();
                c.Delete();
            }
        }



        public void Remove<ComponentType>()
        {
            foreach (KeyValuePair<string, Component> pair in Components)
            {
                if (pair.Value is ComponentType)
                {
                    Remove(pair.Key);
                    return;
                }
            }
        }

        public void Delete()
        {
            m_DeleteFlag = true;
            
        }

        public bool ToDelete()
        {
            if (m_DeleteFlag == true)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


    }

}